<!DOCTYPE html>
<html lang="en">

<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8">
	<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
	
	<!-- title -->
	
	<title>
	
		痛爱IT | 
	 
	小王的博客
	</title>
	
	<!-- keywords,description -->
	 

	<!-- favicon -->
	
	<link rel="shortcut icon" href="/wl/favicon.ico">
	


	<!-- search -->
	<script>
		var searchEngine = "https://www.google.com/search?q=";
		if(typeof searchEngine == "undefined" || searchEngine == null || searchEngine == ""){
			searchEngine = "https://www.google.com/search?q=";
		}
		var homeHost = "wujun234.github.io";
		if(typeof homeHost == "undefined" || homeHost == null || homeHost == ""){
			homeHost = window.location.host;
		}
	</script>


	
<link rel="stylesheet" href="/wl/css/main.css">

	
<link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.min.css">

	
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.17.1/build/styles/darcula.min.css">

	
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css">


	
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>

	
<script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>

	
<script src="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.17.1/build/highlight.min.js"></script>

	
<script src="https://cdn.jsdelivr.net/npm/jquery-pjax@2.0.1/jquery.pjax.min.js"></script>

	
<script src="/wl/js/main.js"></script>

	
		
<script src="https://cdn.jsdelivr.net/npm/leancloud-storage/dist/av-min.js"></script>

		
<script src="https://cdn.jsdelivr.net/npm/valine@v1.4.14/dist/Valine.min.js"></script>

	
	
		<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	
<meta name="generator" content="Hexo 5.4.2"></head>

<body>
	<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?3efe99c287df5a1d6f0d02d187e403c1";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

<header id="header">
    <a id="title" href="/wl/" class="logo">小王的博客</a>

	<ul id="menu">
		<li class="menu-item">
			<a href="/about" class="menu-item-link">ABOUT</a>
		</li>
	

	

		<li class="menu-item">
			<a href="https://github.com/wujun234/uid-generator-spring-boot-starter" class="menu-item-link" target="_blank">
				UidGenerator
			</a>
		</li>
		<li class="menu-item">
			<a href="https://github.com/wujun234" class="menu-item-link" target="_blank">
				<i class="fa fa-github fa-2x"></i>
			</a>
		</li>
	</ul>
</header>

	
<div id="sidebar">
	<button id="sidebar-toggle" class="toggle" ><i class="fa fa-arrow-right " aria-hidden="true"></i></button>
	
	<div id="site-toc">
		<input id="search-input" class="search-input" type="search" placeholder="按回车全站搜索">
		<div id="tree">
			

			
							<ul>
								<li class="file">
									<a href="/wl/2023/02/24/C#%E5%B8%B8%E7%94%A8%E7%9A%84dynamic%20365%E6%96%B9%E6%B3%95/">
                     
										    C#常用的dynamic 365方法
                     
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/wl/2022/11/30/Dynamic-365/">
                     
										    Dynamic-365
                     
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/wl/2023/02/24/Element%20UI%20%E9%80%9F%E9%80%9A/">
                     
										    Element UI 速通
                     
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/wl/2022/11/28/Markdown%E8%AF%AD%E6%B3%95/">
                     
										    Markdown语法
                     
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/wl/2023/02/24/%E5%89%8D%E7%AB%AF%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98/">
                     
										    前端遇到的问题
                     
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/wl/2023/02/24/%E5%B8%B8%E7%94%A8XRM%E5%AF%B9%E8%B1%A1/">
                     
										    常用XRM对象
                     
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/wl/2023/02/24/%E6%8F%92%E4%BB%B6%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98/">
                     
										    插件遇到的问题
                     
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/wl/2023/02/24/%E6%9C%AC%E5%9C%B0%E5%AE%89%E8%A3%85vm%E9%81%87%E5%88%B0%E9%97%AE%E9%A2%98%E7%AC%94%E8%AE%B0/">
                     
										    本地安装vm遇到问题笔记
                     
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file active">
									<a href="/wl/2023/02/24/%E7%97%9B%E7%88%B1IT/">
                     
										    痛爱IT
                     
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
							<ul>
								<li class="file">
									<a href="/wl/2022/11/27/%E8%87%B3%E8%87%AA%E5%B7%B1/">
                     
										    至自己
                     
									</a>
								</li>
								<div class="article-toc" style="display: none;"></div>
							</ul>
			
		</div>
	</div>
</div>

	<!-- 引入正文 -->
	<div id="content">
		<h1 id="article-title">
	痛爱IT
</h1>
<div class="article-meta">
	
	<span>John Doe</span>
	<span>2023-02-24 13:51:13</span>
		<div id="article-categories">
    
		<span>Categories：</span>
            
    

    
		<span>Tags：</span>
            
    
		</div>

</div>

<div id="article-content">
	<h1 id="痛爱IT"><a href="#痛爱IT" class="headerlink" title="痛爱IT"></a>痛爱IT</h1><h3 id="对于查找字段的过滤让其显示为自己相关联的字段"><a href="#对于查找字段的过滤让其显示为自己相关联的字段" class="headerlink" title="对于查找字段的过滤让其显示为自己相关联的字段"></a>对于查找字段的过滤让其显示为自己相关联的字段</h3><p>简单过滤：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">filter</span>(<span class="params"></span>) &#123;</span><br><span class="line">	<span class="title class_">Xrm</span>.<span class="property">Page</span>.<span class="title function_">getControl</span>(<span class="string">&quot;wl_name&quot;</span>).<span class="title function_">addPreSearch</span>(<span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">		<span class="title function_">addLookupFilter</span>();</span><br><span class="line">	&#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">addLookupFilter</span>(<span class="params"></span>) &#123;</span><br><span class="line">	<span class="keyword">var</span> wl_nowclass = <span class="title class_">Xrm</span>.<span class="property">Page</span>.<span class="title function_">getAttribute</span>(<span class="string">&quot;wl_class&quot;</span>).<span class="title function_">getValue</span>(); <span class="comment">//客户类型</span></span><br><span class="line">	<span class="keyword">var</span> fetchXml = <span class="string">&quot;&quot;</span>;</span><br><span class="line">	<span class="keyword">if</span> (wl_nowclass != <span class="literal">null</span>) &#123;</span><br><span class="line">		fetchXml = <span class="string">&quot;&lt;filter type=&#x27;and&#x27;&gt;&lt;condition attribute=&#x27;wl_classname&#x27; operator=&#x27;eq&#x27; value=&#x27;&quot;</span> + wl_nowclass[<span class="number">0</span>].<span class="property">id</span></span><br><span class="line">			.<span class="title function_">replace</span>(<span class="regexp">/\[|]/g</span>, <span class="string">&#x27;&#x27;</span>) + <span class="string">&quot;&#x27; /&gt;&lt;/filter&gt;&quot;</span>;</span><br><span class="line">	&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">		fetchXml = <span class="string">&quot;&lt;filter type=&#x27;and&#x27;&gt;&lt;condition attribute=&#x27;new_special_gl_indicatorid&#x27; operator=&#x27;null&#x27; /&gt;&lt;/filter&gt;&quot;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="title class_">Xrm</span>.<span class="property">Page</span>.<span class="title function_">getControl</span>(<span class="string">&quot;wl_name&quot;</span>).<span class="title function_">addCustomFilter</span>(fetchXml);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="Dynamics-CRM中Xrm-Page-getControl-‘name’-getValue-和-Xrm-Page-getAttribute-‘name’-getValue-的区别"><a href="#Dynamics-CRM中Xrm-Page-getControl-‘name’-getValue-和-Xrm-Page-getAttribute-‘name’-getValue-的区别" class="headerlink" title="Dynamics CRM中Xrm.Page.getControl(‘name’).getValue() 和 Xrm.Page.getAttribute(‘name’).getValue() 的区别"></a>Dynamics CRM中Xrm.Page.getControl(‘name’).getValue() 和 Xrm.Page.getAttribute(‘name’).getValue() 的区别</h3><p>Xrm.Page.getControl(‘name’).getValue() 获取的值是 Xrm.Page.getAttribute(‘name’).getValue() 之前的值。<br>一般情况下，两者的结果一样。<br>但是。<br>假如一个文本字段 name 的当前的值为 value1 , 然后把 A 的值改变为 value2，keydown时，Xrm.Page.getControl(‘name’).getValue() 获取的值是 value2，Xrm.Page.getAttribute(‘name’).getValue() 得到的还是 value1。</p>
<h3 id="ribbon获取当前选中行数以及相应的值"><a href="#ribbon获取当前选中行数以及相应的值" class="headerlink" title="ribbon获取当前选中行数以及相应的值"></a>ribbon获取当前选中行数以及相应的值</h3><p>要从ribbon中转递个selectcontrol</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">SelectNum</span>(<span class="params">selectedControl</span>) &#123;</span><br><span class="line">        <span class="keyword">var</span> selectedRows = selectedControl.<span class="title function_">getGrid</span>().<span class="title function_">getSelectedRows</span>();</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&quot;共选择了&quot;</span> + selectedRows.<span class="title function_">getLength</span>() + <span class="string">&quot;条记录!&quot;</span>);</span><br><span class="line">        <span class="keyword">if</span> (selectedRows.<span class="title function_">getLength</span>() === <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="title class_">Xrm</span>.<span class="property">Navigation</span>.<span class="title function_">openErrorDialog</span>(&#123; <span class="attr">message</span>: <span class="string">&quot;Please select at least one queue item!&quot;</span> &#125;);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span> &#123;</span><br><span class="line">            selectedRows.<span class="title function_">forEach</span>(<span class="keyword">function</span> (<span class="params">row, i</span>) &#123;</span><br><span class="line">                <span class="variable language_">console</span>.<span class="title function_">log</span>(row.<span class="title function_">getData</span>().<span class="property">entity</span>.<span class="property">attributes</span>.<span class="title function_">get</span>(<span class="string">&quot;wl_classname&quot;</span>).<span class="title function_">getValue</span>());</span><br><span class="line">            &#125;);</span><br><span class="line">            <span class="comment">//获得选中的第一条记录某个字段的值</span></span><br><span class="line">            <span class="keyword">var</span> itemCode = selectedRows.<span class="title function_">get</span>(<span class="number">0</span>).<span class="title function_">getData</span>().<span class="property">entity</span>.<span class="property">attributes</span>.<span class="title function_">get</span>(<span class="string">&quot;wl_classname&quot;</span>).<span class="title function_">getValue</span>();</span><br><span class="line">            <span class="variable language_">console</span>.<span class="title function_">log</span>(itemCode);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="自定义工作流"><a href="#自定义工作流" class="headerlink" title="自定义工作流"></a>自定义工作流</h3><h4 id="需要应用using集合"><a href="#需要应用using集合" class="headerlink" title="需要应用using集合"></a>需要应用using集合</h4><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> Microsoft.Xrm.Sdk.Workflow;</span><br><span class="line"><span class="keyword">using</span> Microsoft.Xrm.Sdk;</span><br></pre></td></tr></table></figure>

<h4 id="在自定义工作流中必填值为"><a href="#在自定义工作流中必填值为" class="headerlink" title="在自定义工作流中必填值为"></a>在自定义工作流中必填值为</h4><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="meta">RequiredArgument</span>]</span><br></pre></td></tr></table></figure>

<h4 id="如果未定义作为输入参数传入的值或设置为输出参数的值，则可以指定默认值。例如，要设置-bool-属性的默认值"><a href="#如果未定义作为输入参数传入的值或设置为输出参数的值，则可以指定默认值。例如，要设置-bool-属性的默认值" class="headerlink" title="如果未定义作为输入参数传入的值或设置为输出参数的值，则可以指定默认值。例如，要设置 bool 属性的默认值"></a>如果未定义作为输入参数传入的值或设置为输出参数的值，则可以指定默认值。例如，要设置 bool 属性的默认值</h4><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[<span class="meta">Input(<span class="string">&quot;Bool input&quot;</span>)</span>]</span><br><span class="line">[<span class="meta">Default(<span class="string">&quot;True&quot;</span>)</span>]</span><br><span class="line"><span class="keyword">public</span> InArgument&lt;<span class="built_in">bool</span>&gt; Bool &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br></pre></td></tr></table></figure>

<h4 id="默认值的格式取决于属性的类型"><a href="#默认值的格式取决于属性的类型" class="headerlink" title="默认值的格式取决于属性的类型"></a>默认值的格式取决于属性的类型</h4><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">bool</span>	[Default(<span class="string">&quot;True&quot;</span>)]</span><br><span class="line">DateTime	[Default(<span class="string">&quot;2004-07-09T02:54:00Z&quot;</span>)]</span><br><span class="line">Decimal	[Default(<span class="string">&quot;23.45&quot;</span>)]</span><br><span class="line">Double	[Default(<span class="string">&quot;23.45&quot;</span>)]</span><br><span class="line">Money	[Default(<span class="string">&quot;23.45&quot;</span>)]</span><br><span class="line">EntityReference	[Default(<span class="string">&quot;3B036E3E-94F9-DE11-B508-00155DBA2902&quot;</span>, <span class="string">&quot;account&quot;</span>)]</span><br><span class="line"><span class="built_in">int</span>	[Default(<span class="string">&quot;23&quot;</span>)]</span><br><span class="line">OptionSetValue	[Default(<span class="string">&quot;3&quot;</span>)]</span><br><span class="line"><span class="built_in">string</span>	[Default(<span class="string">&quot;string default&quot;</span>)]</span><br></pre></td></tr></table></figure>

<h4 id="当接受值类型为Entity-Reference时需要用到"><a href="#当接受值类型为Entity-Reference时需要用到" class="headerlink" title="当接受值类型为Entity Reference时需要用到"></a>当接受值类型为Entity Reference时需要用到</h4><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[<span class="meta">ReferenceTarget(<span class="string">&quot;account&quot;</span>)</span>]</span><br></pre></td></tr></table></figure>

<h4 id="为-Option-Set-Value-参数定义属性时，必须使用该属性。此属性定义哪个表和列包含参数的有效值集"><a href="#为-Option-Set-Value-参数定义属性时，必须使用该属性。此属性定义哪个表和列包含参数的有效值集" class="headerlink" title="为 Option Set Value 参数定义属性时，必须使用该属性。此属性定义哪个表和列包含参数的有效值集"></a>为 <a target="_blank" rel="noopener" href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.optionsetvalue">Option Set Value</a> 参数定义属性时，必须使用该属性。此属性定义哪个表和列包含参数的有效值集</h4><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">[<span class="meta">Input(<span class="string">&quot;Account IndustryCode value&quot;</span>)</span>]</span><br><span class="line">[<span class="meta">AttributeTarget(<span class="string">&quot;account&quot;</span>, <span class="string">&quot;industrycode&quot;</span>)</span>]</span><br><span class="line">[<span class="meta">Default(<span class="string">&quot;3&quot;</span>)</span>]</span><br><span class="line"><span class="keyword">public</span> InArgument&lt;OptionSetValue&gt; IndustryCode &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br></pre></td></tr></table></figure>



<h4 id="在Vs-2019中选择活动选择代码活动。"><a href="#在Vs-2019中选择活动选择代码活动。" class="headerlink" title="在Vs 2019中选择活动选择代码活动。"></a>在Vs 2019中选择活动选择代码活动。</h4><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 定义一个字符串类型的活动输入参数</span></span><br><span class="line"> [<span class="meta">Input(<span class="string">&quot;Text&quot;</span>)</span>]</span><br><span class="line"> <span class="keyword">public</span> InArgument&lt;<span class="built_in">string</span>&gt; Text &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 定义一个字符串类型的活动输出参数</span></span><br><span class="line"> [<span class="meta">Output(<span class="string">&quot;OutText&quot;</span>)</span>]</span><br><span class="line"> <span class="keyword">public</span> OutArgument&lt;<span class="built_in">string</span>&gt; OutText &#123; <span class="keyword">get</span>; <span class="keyword">set</span>; &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">// 如果活动返回值，则从 CodeActivity&lt;TResult&gt;</span></span><br><span class="line"> <span class="comment">// 并从 Execute 方法返回该值。</span></span><br><span class="line"> <span class="function"><span class="keyword">protected</span> <span class="keyword">override</span> <span class="keyword">void</span> <span class="title">Execute</span>(<span class="params">CodeActivityContext context</span>)</span></span><br><span class="line"> &#123;</span><br><span class="line">     <span class="comment">// 获取 Text 输入参数的运行时值</span></span><br><span class="line">     <span class="built_in">string</span> text = context.GetValue(<span class="keyword">this</span>.Text);</span><br><span class="line">     <span class="comment">//组织活动</span></span><br><span class="line">     IWorkflowContext workflowContext = context.GetExtension&lt;IWorkflowContext&gt;();</span><br><span class="line">     IOrganizationServiceFactory factory = context.GetExtension&lt;IOrganizationServiceFactory&gt;();</span><br><span class="line">     IOrganizationService service = factory.CreateOrganizationService(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">     Guid id = Guid.Empty;</span><br><span class="line">     Entity entity = <span class="keyword">new</span> Entity(<span class="string">&quot;wl_studentinformation&quot;</span>);</span><br><span class="line">     entity.Attributes.Add(<span class="string">&quot;wl_name&quot;</span>, Text.Get(context));</span><br><span class="line">     id = service.Create(entity);</span><br><span class="line">     OutText.Set(context, id.ToString());</span><br></pre></td></tr></table></figure>

<p>在插件发布不用配置相应的触发事件。</p>
<p>在环境中用工作流的方式去配置。</p>
<h3 id="Action用js调用"><a href="#Action用js调用" class="headerlink" title="Action用js调用"></a>Action用js调用</h3><h4 id="dynamic-365"><a href="#dynamic-365" class="headerlink" title="dynamic 365"></a>dynamic 365</h4><p><img src="C:\Users\29310\AppData\Roaming\Typora\typora-user-images\image-20230130172715174.png" alt="image-20230130172715174"></p>
<h4 id="C-部分"><a href="#C-部分" class="headerlink" title="C#部分"></a>C#部分</h4><p>引用</p>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> Microsoft.Xrm.Sdk;</span><br></pre></td></tr></table></figure>

<p>配置</p>
<figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">testAction</span> : <span class="title">IPlugin</span></span><br><span class="line">   &#123;</span><br><span class="line">       <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Execute</span>(<span class="params">IServiceProvider serviceProvider</span>)</span></span><br><span class="line">       &#123;</span><br><span class="line">           IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(<span class="keyword">typeof</span>(IPluginExecutionContext));</span><br><span class="line">           IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(<span class="keyword">typeof</span>(IOrganizationServiceFactory));</span><br><span class="line">           IOrganizationService service = serviceFactory.CreateOrganizationService(<span class="literal">null</span>); <span class="comment">// 权限</span></span><br><span class="line"></span><br><span class="line">           <span class="built_in">string</span> name = context.InputParameters[<span class="string">&quot;name&quot;</span>].ToString();</span><br><span class="line">           <span class="built_in">bool</span> sex = Convert.ToBoolean(context.InputParameters[<span class="string">&quot;sex&quot;</span>]);</span><br><span class="line"></span><br><span class="line">           Entity entity = <span class="keyword">new</span> Entity(<span class="string">&quot;wl_studentinformation&quot;</span>);</span><br><span class="line">           entity[<span class="string">&quot;wl_name&quot;</span>] = name;</span><br><span class="line">           entity[<span class="string">&quot;wl_sex&quot;</span>] = sex;</span><br><span class="line">           <span class="comment">//entity[&quot;new_contact&quot;] = entityRef;</span></span><br><span class="line">           service.Create(entity);</span><br><span class="line"></span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<h4 id="js部分"><a href="#js部分" class="headerlink" title="js部分"></a>js部分</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">	<span class="keyword">var</span> parameters = <span class="keyword">new</span> <span class="title class_">Object</span>();</span><br><span class="line">	parameters.<span class="property">name</span> = <span class="string">&quot;王五&quot;</span>;</span><br><span class="line">	parameters.<span class="property">sex</span> = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">var</span> req = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>();</span><br><span class="line">	req.<span class="title function_">open</span>(<span class="string">&quot;POST&quot;</span>, <span class="title class_">Xrm</span>.<span class="property">Page</span>.<span class="property">context</span>.<span class="title function_">getClientUrl</span>() + <span class="string">&quot;/api/data/v9.0/new_studentsaction&quot;</span>, <span class="literal">true</span>);</span><br><span class="line">	req.<span class="title function_">setRequestHeader</span>(<span class="string">&quot;OData-MaxVersion&quot;</span>, <span class="string">&quot;4.0&quot;</span>);</span><br><span class="line">	req.<span class="title function_">setRequestHeader</span>(<span class="string">&quot;OData-Version&quot;</span>, <span class="string">&quot;4.0&quot;</span>);</span><br><span class="line">	req.<span class="title function_">setRequestHeader</span>(<span class="string">&quot;Accept&quot;</span>, <span class="string">&quot;application/json&quot;</span>);</span><br><span class="line">	req.<span class="title function_">setRequestHeader</span>(<span class="string">&quot;Content-Type&quot;</span>, <span class="string">&quot;application/json; charset=utf-8&quot;</span>);</span><br><span class="line">	req.<span class="property">onreadystatechange</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">		<span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">readyState</span> === <span class="number">4</span>) &#123;</span><br><span class="line">			req.<span class="property">onreadystatechange</span> = <span class="literal">null</span>;</span><br><span class="line">			<span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">status</span> === <span class="number">204</span>) &#123;</span><br><span class="line">				<span class="title function_">alert</span>(<span class="string">&quot;成功&quot;</span>);</span><br><span class="line">				<span class="comment">//Success - No Return Data - Do Something</span></span><br><span class="line">			&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">				<span class="keyword">var</span> exper = <span class="title class_">JSON</span>.<span class="title function_">parse</span>(req.<span class="property">responseText</span>);</span><br><span class="line">				<span class="title class_">Xrm</span>.<span class="property">Utility</span>.<span class="title function_">alertDialog</span>(exper.<span class="property">error</span>.<span class="property">message</span>);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;;</span><br><span class="line">	req.<span class="title function_">send</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(parameters));</span><br><span class="line">&#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">	<span class="title function_">alert</span>(e.<span class="property">message</span>);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>通过try的方式抛出的异常可以更加详细而不是bad request，导致用户无法判断。</p>
<h3 id="XMLHttpRequest状态码"><a href="#XMLHttpRequest状态码" class="headerlink" title="XMLHttpRequest状态码"></a>XMLHttpRequest状态码</h3><p>readyState状态码</p>
<ol>
<li>0：未初始化，还没有调用send()方法；</li>
<li>1：载入，已调用send()方法，正在发送请求；</li>
<li>2：载入完成，send()方法执行完成，已经接收到全部响应内容；</li>
<li>3：交互，正在解析响应内容；</li>
<li>4：完成，响应内容解析完成，可以在客户端进行调用了；</li>
</ol>
<p>status状态码</p>
<ol>
<li>200 get 成功</li>
<li>201 post 成功</li>
<li>204-无内容</li>
<li>301 永久重定向</li>
<li>302 临时重定向</li>
<li>304 协商缓存 服务器文件未修改</li>
<li>400 客户端请求有语法错误，不能被服务器识别</li>
<li>403 服务器受到请求，但是拒绝提供服务，可能是跨域</li>
<li>404 请求的资源不存在</li>
<li>405 请求的method不允许</li>
<li>500 服务器发生不可预期的错误</li>
</ol>
<h3 id="备用键"><a href="#备用键" class="headerlink" title="备用键"></a>备用键</h3><p>设置为备用键后的字段，字段值不能重复，还可以通过代码修改记录值，</p>
<h4 id="C-部分-1"><a href="#C-部分-1" class="headerlink" title="C#部分"></a>C#部分</h4><figure class="highlight c#"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">try</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">var</span> testEntity = <span class="keyword">new</span> Entity(<span class="string">&quot;wl_studentachievement&quot;</span>, <span class="keyword">new</span> KeyAttributeCollection &#123; &#123; <span class="string">&quot;wl_examinationsubjects&quot;</span>, <span class="string">&quot;王磊测试记录一修改66&quot;</span> &#125; &#125;);</span><br><span class="line">    testEntity[<span class="string">&quot;wl_examinationsubjects&quot;</span>] = <span class="string">&quot;王磊测试记录一修改&quot;</span>;</span><br><span class="line">    testEntity[<span class="string">&quot;wl_examinationresults&quot;</span>] = <span class="number">80</span>;</span><br><span class="line">    </span><br><span class="line">    proxy.Update(testEntity);</span><br><span class="line">    <span class="comment"><span class="doctag">///</span>/请记住键名使用的是备用键字段名，而不是备用键的名称</span></span><br><span class="line">    <span class="comment">//testEntity = new Entity(&quot;ly_test&quot;, new KeyAttributeCollection &#123; &#123; &quot;ly_alternatekey&quot;, &quot;13926492255&quot; &#125; &#125;);</span></span><br><span class="line">    <span class="comment">//testEntity[&quot;ly_name&quot;] = &quot;测试记录二修改&quot;;</span></span><br><span class="line">    <span class="comment">//proxy.Update(testEntity);</span></span><br><span class="line">    <span class="keyword">var</span> testSubEntity = <span class="keyword">new</span> Entity(<span class="string">&quot;ly_testsub&quot;</span>);</span><br><span class="line">    testSubEntity[<span class="string">&quot;ly_name&quot;</span>] = <span class="string">&quot;测试使用备用键来创建查找字段的值&quot;</span>;</span><br><span class="line">    testSubEntity[<span class="string">&quot;ly_test&quot;</span>] = <span class="keyword">new</span> EntityReference(<span class="string">&quot;ly_test&quot;</span>, <span class="string">&quot;ly_alternatekey&quot;</span>, <span class="string">&quot;13926492254&quot;</span>);</span><br><span class="line">    proxy.Create(testSubEntity);</span><br><span class="line">    Console.WriteLine(<span class="string">&quot;程序运行完成&quot;</span>);</span><br><span class="line">    Console.ReadKey();</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">catch</span> (FaultException ex)</span><br><span class="line">&#123;</span><br><span class="line">    Console.WriteLine(<span class="string">&quot;程序出现异常:ex.Message=&quot;</span> + ex.Message);</span><br><span class="line">    Console.WriteLine(<span class="string">&quot;ex.StackTrace=&quot;</span> + ex.StackTrace);</span><br><span class="line">    Console.ReadKey();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="通过api的方式"><a href="#通过api的方式" class="headerlink" title="通过api的方式"></a>通过api的方式</h4><p>搜索</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> req = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>();</span><br><span class="line">req.<span class="title function_">open</span>(<span class="string">&quot;GET&quot;</span>, <span class="title class_">Xrm</span>.<span class="property">Page</span>.<span class="property">context</span>.<span class="title function_">getClientUrl</span>() + <span class="string">&quot;/api/data/v9.0/wl_studentachievements(wl_examinationsubjects=&#x27;小学英语&#x27;)?$select=wl_examinationsubjects&quot;</span>, <span class="literal">true</span>);</span><br><span class="line">req.<span class="title function_">setRequestHeader</span>(<span class="string">&quot;OData-MaxVersion&quot;</span>, <span class="string">&quot;4.0&quot;</span>);</span><br><span class="line">req.<span class="title function_">setRequestHeader</span>(<span class="string">&quot;OData-Version&quot;</span>, <span class="string">&quot;4.0&quot;</span>);</span><br><span class="line">req.<span class="title function_">setRequestHeader</span>(<span class="string">&quot;Accept&quot;</span>, <span class="string">&quot;application/json&quot;</span>);</span><br><span class="line">req.<span class="title function_">setRequestHeader</span>(<span class="string">&quot;Content-Type&quot;</span>, <span class="string">&quot;application/json; charset=utf-8&quot;</span>);</span><br><span class="line">req.<span class="title function_">setRequestHeader</span>(<span class="string">&quot;Prefer&quot;</span>, <span class="string">&quot;odata.include-annotations=\&quot;*\&quot;&quot;</span>);</span><br><span class="line">req.<span class="property">onreadystatechange</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">readyState</span> === <span class="number">4</span>) &#123;</span><br><span class="line">        req.<span class="property">onreadystatechange</span> = <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">status</span> === <span class="number">200</span>) &#123;</span><br><span class="line">            <span class="keyword">var</span> result = <span class="title class_">JSON</span>.<span class="title function_">parse</span>(<span class="variable language_">this</span>.<span class="property">response</span>);</span><br><span class="line">            <span class="keyword">var</span> wl_examinationsubjects = result[<span class="string">&quot;wl_examinationsubjects&quot;</span>];</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="title class_">Xrm</span>.<span class="property">Utility</span>.<span class="title function_">alertDialog</span>(<span class="variable language_">this</span>.<span class="property">statusText</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">req.<span class="title function_">send</span>();</span><br></pre></td></tr></table></figure>

<p>修改</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> entity = &#123;&#125;;</span><br><span class="line">entity.<span class="property">wl_examinationsubjects</span> = <span class="string">&quot;王磊测试记录api修改&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> req = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>();</span><br><span class="line">req.<span class="title function_">open</span>(<span class="string">&quot;PATCH&quot;</span>, <span class="title class_">Xrm</span>.<span class="property">Page</span>.<span class="property">context</span>.<span class="title function_">getClientUrl</span>() + <span class="string">&quot;/api/data/v9.0/wl_studentachievements(wl_examinationsubjects=&#x27;王磊测试记录一修改&#x27;)&quot;</span>, <span class="literal">true</span>);</span><br><span class="line">req.<span class="title function_">setRequestHeader</span>(<span class="string">&quot;OData-MaxVersion&quot;</span>, <span class="string">&quot;4.0&quot;</span>);</span><br><span class="line">req.<span class="title function_">setRequestHeader</span>(<span class="string">&quot;OData-Version&quot;</span>, <span class="string">&quot;4.0&quot;</span>);</span><br><span class="line">req.<span class="title function_">setRequestHeader</span>(<span class="string">&quot;Accept&quot;</span>, <span class="string">&quot;application/json&quot;</span>);</span><br><span class="line">req.<span class="title function_">setRequestHeader</span>(<span class="string">&quot;Content-Type&quot;</span>, <span class="string">&quot;application/json; charset=utf-8&quot;</span>);</span><br><span class="line">req.<span class="property">onreadystatechange</span> = <span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">readyState</span> === <span class="number">4</span>) &#123;</span><br><span class="line">        req.<span class="property">onreadystatechange</span> = <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">status</span> === <span class="number">204</span>) &#123;</span><br><span class="line">            <span class="comment">//Success - No Return Data - Do Something</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="title class_">Xrm</span>.<span class="property">Utility</span>.<span class="title function_">alertDialog</span>(<span class="variable language_">this</span>.<span class="property">statusText</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line">req.<span class="title function_">send</span>(<span class="title class_">JSON</span>.<span class="title function_">stringify</span>(entity));</span><br></pre></td></tr></table></figure>


</div>


    <div class="post-guide">
        <div class="item left">
            
              <a href="/wl/2023/02/24/Element%20UI%20%E9%80%9F%E9%80%9A/">
                  <i class="fa fa-angle-left" aria-hidden="true"></i>
                  Element UI 速通
              </a>
            
        </div>
        <div class="item right">
            
              <a href="/wl/2023/02/24/%E5%89%8D%E7%AB%AF%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98/">
                
                <i class="fa fa-angle-right" aria-hidden="true"></i>
              </a>
            
        </div>
    </div>



	<div id="vcomments"></div>


<script>
	
</script>
	</div>
	<div id="footer">
	<p>
	©2019-<span id="footerYear"></span> 
	<a href="/">John Doe</a> 
	
	
		|
		<span id="busuanzi_container_site_pv">
			pv
			<span id="busuanzi_value_site_pv"></span>
		</span>
		|
		<span id="busuanzi_container_site_uv"> 
			uv
			<span id="busuanzi_value_site_uv"></span>
		</span>
	
	<br>
	Theme <a href="//github.com/wujun234/hexo-theme-tree" target="_blank">Tree</a>
	by <a href="//github.com/wujun234" target="_blank">WuJun</a>
	Powered by <a href="//hexo.io" target="_blank">Hexo</a>
	</p>
</div>
<script type="text/javascript"> 
	document.getElementById('footerYear').innerHTML = new Date().getFullYear() + '';
</script>
	<button id="totop-toggle" class="toggle"><i class="fa fa-angle-double-up" aria-hidden="true"></i></button>
</body>
</html>